﻿using JiveMessenger.Models;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace JiveMessenger.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class InviteToGroupPage : BasicPage
    {
        public InviteToGroupPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            // this.TextBoxPageHeader.Opacity = 0;
            base.OnNavigatedTo(e);
            ViewModelLocator locator = Application.Current.Resources["Locator"] as ViewModelLocator;
            this.DataContext = new ViewModels.ViewModelInviteContactToGroup(locator.CurrentClient, locator.NavService, e.Parameter as JiveChatGroupModel);


            //if (e.Parameter == null || (bool)e.Parameter == false)
            //{
            StatusBar.GetForCurrentView().HideAsync();
            //    await Task.Delay(800);
            //    Storyboard sb = this.Resources["ShowPageHeaderAnim"] as Storyboard;
            //    sb.Begin();
            //}
            //else
            //{
            this.TextBoxPageHeader.Opacity = 1;
            //}
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView lv = (ListView)sender;
            foreach (AddContactToGroupModel model in e.AddedItems)
            {
                model.IsChecked = true;
            }
            foreach (AddContactToGroupModel model in e.RemovedItems)
            {
                model.IsChecked = false;
            }
            ((ViewModelInviteContactToGroup)this.DataContext).InviteContactsToGroupCommand.RaiseCanExecuteChanged();
        }
        private void BasicPage_Loaded(object sender, RoutedEventArgs e)
        {
            Window.Current.Activated += Current_Activated;
        }

        private void BasicPage_Unloaded(object sender, RoutedEventArgs e)
        {
            Window.Current.Activated -= Current_Activated;
        }

        async void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
                this.TextBoxPageHeader.Opacity = 0;
            else
            {
                await StatusBar.GetForCurrentView().HideAsync();
                await Task.Delay(800);
                Storyboard sb = this.Resources["ShowPageHeaderAnim"] as Storyboard;
                sb.Begin();
            }
        }
    }
}
